<?php
require_once './common.php';

use JsonDb\JsonDb\Db;
//退出登录
if ($_GET['type'] == 'quit') {
    session_start();

    // 删除 $_SESSION['user']
    unset($_SESSION['user']);

    // 现在 $_SESSION['user'] 已经被删除，尝试访问它将返回 null
    if (!isset($_SESSION['user'])) {
        setcookie('status', '注销成功', time() + 3); // 
        header("Location: /admin/login.php");
    } else {
        setcookie('status', '注销失败', time() + 3); // 
        header('Location: ' . $_SERVER['HTTP_REFERER']);
    }
    exit;
}
if ($_GET['type'] == 'class_delete') {
    if (isset($_GET['id'])) {
        $rst = DB::name('class')->where('id', $_GET['id'])->deleteAll();
        if ($rst) {
            setcookie('status', '删除成功！', time() + 3); // 
        } else {
            setcookie('status', '删除失败！', time() + 3); // 
        }
        header("Location: /admin/class.php");
        exit();
    }
}




//图标删除
if ($_GET['type'] == 'svg_delete') {
    if (isset($_GET['id'])) {
        $rst = DB::name('svg')->where('id', $_GET['id'])->deleteAll();
        if ($rst) {
            setcookie('status', '删除成功！', time() + 3); // 
        } else {
            setcookie('status', '删除失败！', time() + 3); // 
        }
        header("Location: /admin/svg.php");
        exit();
    }
}
//图标修改
if ($_GET['type'] == 'svg_edit') {
    if (isset($_GET['id'])) {
        $data = [
            'url' => $_POST['url'],

            'svg' => $_POST['svg'],



        ];

        if (empty($_POST['svg'])) {
            setcookie('status', '图标不能为空', time() + 3); // 
            header('Location: ' . $_SERVER['HTTP_REFERER']);
            exit;
        }
        $rst = Db::name('svg')->where('id', $_GET['id'])->update($data);
        if ($rst) {
            setcookie('status', '更新成功！', time() + 3); // 
        } else {
            setcookie('status', '更新失败！', time() + 3); // 
        }
        header("Location: /admin/svg.php");
        exit();
    }
}
//历程修改
if ($_GET['type'] == 'lc_edit') {
    if (isset($_GET['id'])) {
        $data = [
            'time' => $_POST['time'],
            'name' => $_POST['name'],
        ];

        if (empty($_POST['time'])) {
            setcookie('status', '历程时间不能为空', time() + 3); // 
            header('Location: ' . $_SERVER['HTTP_REFERER']);
            exit;
        }
        if (empty($_POST['name'])) {
            setcookie('status', '历程事件不能为空', time() + 3); // 
            header('Location: ' . $_SERVER['HTTP_REFERER']);
            exit;
        }
        $rst = Db::name('lc')->where('id', $_GET['id'])->update($data);
        if ($rst) {
            setcookie('status', '更新成功！', time() + 3); // 
        } else {
            setcookie('status', '更新失败！', time() + 3); // 
        }
        header("Location: /admin/lc.php");
        exit();
    }
}
//历程删除
if ($_GET['type'] == 'lc_delete') {
    if (isset($_GET['id'])) {
        $rst = DB::name('lc')->where('id', $_GET['id'])->deleteAll();
        if ($rst) {
            setcookie('status', '删除成功！', time() + 3); // 
        } else {
            setcookie('status', '删除失败！', time() + 3); // 
        }
        header("Location: /admin/lc.php");
        exit();
    }
}
//站点修改
if ($_GET['type'] == 'zd_edit') {
    if (isset($_GET['id'])) {
        $data = [
            'content' => $_POST['content'],
            'name' => $_POST['name'],
            'url' => $_POST['url'],
            'status' => $_POST['status'],
        ];

        if (empty($_POST['name'])) {
            setcookie('status', '站点名称不能为空', time() + 3); // 
            header('Location: ' . $_SERVER['HTTP_REFERER']);
            exit;
        }
        if (empty($_POST['url'])) {
            setcookie('status', '站点url不能为空', time() + 3); // 
            header('Location: ' . $_SERVER['HTTP_REFERER']);
            exit;
        }
        $rst = Db::name('zd')->where('id', $_GET['id'])->update($data);
        if ($rst) {
            setcookie('status', '更新成功！', time() + 3); // 
        } else {
            setcookie('status', '更新失败！', time() + 3); // 
        }
        header("Location: /admin/zd.php");
        exit();
    }
}

//历程删除
if ($_GET['type'] == 'zd_delete') {
    if (isset($_GET['id'])) {
        $rst = DB::name('zd')->where('id', $_GET['id'])->deleteAll();
        if ($rst) {
            setcookie('status', '删除成功！', time() + 3); // 
        } else {
            setcookie('status', '删除失败！', time() + 3); // 
        }
        header("Location: /admin/zd.php");
        exit();
    }
}
